/**

  @file Main.c
  
	Programmer: Heeyoung Choi
	Start Date: 23-MAY-2013
	Revision Date: 
	Description:
	Reviewer:
	Reviewing Data:
	Version: 
*/

/*******************************************************************************
**
**  

This device driver was created by Applilet2 for V850ES/Fx3
**  32-Bit Single-Chip Microcontrollers
**
**  Copyright(C) 2002, 2013 Renesas Electronics Corporation
**  All rights reserved by Renesas Electronics Corporation.
**
**  This program should be used on your own responsibility.
**  Renesas Electronics Corporation assumes no responsibility for any losses
**  incurred by customers or third parties arising from the use of this file.
**
**  Filename :	 main.c
**  Abstract :	This file implements main function.
**  APIlib :	Applilet2 for V850ES/Fx3 V2.43 [30 Mar 2011]
**
**  Device :	uPD70F3376
**
**  Compiler :	CA850
**
**  Creation date:	2013-05-09
**  
*******************************************************************************
*/
#pragma ioreg

/*
*******************************************************************************
**  Include files
*******************************************************************************
*/

#include "macrodriver.h"
#include "serial.h"
#include "system.h"
/* Start user code for include definition. Do not edit comment generated here */
/* End user code for include definition. Do not edit comment generated here */
#include "user_define.h"
#include "spi_if.h"
#include "data_flash.h"
#include "bq_pack.h"

#pragma interrupt INTCB1R	isr_INTCB1R

/*
*******************************************************************************
**  Global define
*******************************************************************************
*/
/* Start user code for global definition. Do not edit comment generated here */
/* End user code for global definition. Do not edit comment generated here */

extern unsigned long _S_romp;  // Rom  

/*
**-----------------------------------------------------------------------------
**
**	Abstract:
**		This function implements main function.
**
**	Parameters:
**		None
**
**	Returns:
**		None
**
**-----------------------------------------------------------------------------
*/

#define CB1_RX_LENTH	0x7f

unsigned char	cb1_rx_head	=	0;
unsigned char	cb1_rx_tail	=	0;
unsigned char	cb1_rx_buf[CB1_RX_LENTH];

unsigned int INT=0;

__interrupt void isr_INTCB1R(void)
{
	cb1_rx_buf[cb1_rx_head++]	=	CB1RXL;
	if(cb1_rx_head > CB1_RX_LENTH)	cb1_rx_head	=	0;
}

int check_cb1_rx_buf(void)
{
	if(cb1_rx_head != cb1_rx_tail)	return 1;
	else 							return 0;
}

void Port_G9_Init(void)
{
	P9L = 0x40;
	PM9L = 0xBF;
	PMC9L = 0x00;
}

short csi_read_reg(unsigned char dev_addr, unsigned char reg_addr, short elem_num, unsigned char discard_crc, //*True, False*/
                   unsigned char* pData)
{
  unsigned char package[3];
  short i=0;
  unsigned char TimeOutCounter = 0;

    package[0] = (dev_addr<<1)/*Read*/;
    package[1] = reg_addr;
    package[2] = elem_num;

   //Write 1 byte into the selected register

  //  STE_PxOUT &= ~IO_SPI_CS;                   // Enable BQ Pack, /CS asserted

	return 1;
}

unsigned char get_cb1_rx_byte(void)
{
	unsigned char	data;

	data	=	cb1_rx_buf[cb1_rx_tail++];
	if(cb1_rx_tail > CB1_RX_LENTH)	cb1_rx_tail	=	0;
	return	data;
}

short csi_write_reg (unsigned char dev_addr, unsigned char reg_addr, unsigned char data)
{
	unsigned char package[4];
	unsigned char TimeOutCounter = 0;
	short i = 0;

    package[0] = (dev_addr<<1) | 0x01/*Write*/;
    package[1] = reg_addr;
    package[2] = data;
    package[3] = calculate_crc(package, 3);

   //Write 1 byte into the selected register

	P9L.6 = 0;

  //  STE_PxOUT &= ~IO_SPI_CS;                   // Enable BQ Pack, /CS asserted

    /* Check TX buffer. Ready ? */
	return 1;
}

void put_cb1_tx_byte(unsigned char data)
{ 

	CB1TX	=	data;
	while(CB1TSF);
}

	unsigned char cpack=0;
	unsigned char Adata[2];
	unsigned char Cell_V[6];
	
	unsigned char Dev_Cell_V[6];
	unsigned char Cell_Ctr[1]={0};

void  main(void)
{
	/* Start user code. Do not edit comment generated here */

	int i, j;

	unsigned char idata,jdata,kdata;
	unsigned char ndata[4];
	unsigned char S_Conv, S_Start, ADC_CNT, CB_BAL_ON, CB_BAL_OFF, CB_TIME;

    unsigned char reg_val1[2];
    unsigned char reg_val2[2];
    unsigned char reg_val3[2];
    unsigned char reg_val4[2];
    unsigned char *pData;
    unsigned char *pPtr;
    unsigned char temp;

   unsigned char Pack_On[6] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20};
   unsigned char Pack_Off[6] = {0x3E, 0x3C, 0x3B, 0x37, 0x2F, 0x1F};
   unsigned char Pack_Ctr[1]={0};

    unsigned long voltage_comput;
    char p_char;

	unsigned char  ADDR, STATUS_REG, ADDRR, ADDR_T;
	short ret_val;

	bq_dev_t bq_devs;

	ADDR = 0x00;
	STATUS_REG = 0x00;
	ADDRR = 0x3b;
	S_Conv = 0x34;
	S_Start = 0x01;
	ADDR_T = 0x01;
	ADC_CNT = 0x05;	
	CB_BAL_ON = 0x3F;    // 0011 1111
	CB_BAL_OFF = 0x00;
	CB_TIME = 0x9E;
	
	jdata = 4;
	kdata = 0x11;

	Adata[0]=0x02;
	Adata[1]=0x04;

   Port_G9_Init();
   SystemInit();

   CSIB1_Start();

   idata = 0x37;

	bq_pack_init();

	for (i=0; i <5000;i++);

     bq_dev_write_reg(ADDR, ADDRR, ADDR_T);
 
		for (i=0; i <5000;i++);

        bq_dev_write_reg(ADDR_T,  CB_TIME_REG, CB_TIME);
 
		for (i=0; i <5000;i++);

        bq_dev_write_reg(ADDR_T,  CB_CTRL_REG, CB_BAL_OFF);
   
		for (i=0; i <5000;i++);

         bq_dev_read_reg(ADDR_T,CB_CTRL_REG, 2, DISCARD_CRC, reg_val3);
   
		for (i=0; i <5000;i++);

		bq_dev_write_reg(ADDR_T, ADC_CONTROL_REG, ADC_CNT);

		for (i=0; i <5000;i++);	 	   

 	   bq_dev_read_reg(ADDR_T,ADC_CONTROL_REG, 2, DISCARD_CRC, reg_val4);

		for (i=0; i <5000;i++);

	 bq_dev_write_reg(BROADCAST_ADDR, RESET_REG, BQ76PL536_RESET);



		for (j=0; j <5000;j++);

	while (1) {

		bq_dev_write_reg(ADDR_T, S_Conv, S_Start);
		
       bq_dev_read_reg(ADDR_T,VCELL1_L_REG, 1, DISCARD_CRC, &Cell_V[0]);

        for (j=0; j <5000;j++);

        bq_dev_read_reg(ADDR_T,VCELL1_H_REG, 1, DISCARD_CRC, &Cell_V[1]);

        for (j=0; j <5000;j++);
		

//		bq_dev_read_cell_voltage(&bq_pack.bq_devs[0]);
	}
	/* End user code. Do not edit comment generated here */
}

/* Start adding user code. Do not edit comment generated here */
/* End user code adding. Do not edit comment generated here */
